package com.sikaryofficial.system.controller;

import cn.hutool.core.collection.CollUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.sikaryofficial.common.core.domain.R;
import com.sikaryofficial.common.core.exception.ServiceException;
import com.sikaryofficial.common.core.utils.poi.ExcelUtil;
import com.sikaryofficial.common.core.web.controller.BaseController;
import com.sikaryofficial.common.core.web.page.TableDataInfo;
import com.sikaryofficial.common.log.annotation.Log;
import com.sikaryofficial.common.log.enums.BusinessType;
import com.sikaryofficial.system.api.RemoteWorkBenchService;
import com.sikaryofficial.system.api.model.SysMemberReq;
import com.sikaryofficial.system.config.HiSMKAuthConfig;
import com.sikaryofficial.system.domain.ChannelGoodsScopeReq;
import com.sikaryofficial.system.domain.dto.SysChannelUserListDTO;
import com.sikaryofficial.system.domain.dto.SysMemberUserListReq;
import com.sikaryofficial.system.manager.UserChannelManager;
import com.sikaryofficial.system.service.ISysMemberUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 渠道商管理
 *
 * @author qinjinyuan
 */
@RestController
@RequestMapping("/channel")
@Api(tags = "渠道商管理")
public class SysChannelUserController extends BaseController {
    @Autowired
    private ISysMemberUserService memberUserService;
    @Resource
    private HiSMKAuthConfig authConfig;
    @Autowired
    private UserChannelManager goodsScopeManager;
    @Autowired
    private RemoteWorkBenchService remoteWorkBenchService;
    @PostMapping("/allotGoodsScope")
    @ApiOperation(value = "渠道商-可见范围指定")
    public R<Boolean> allotGoodsScope(@Validated @RequestBody ChannelGoodsScopeReq req) {
        // 缓存清除
        remoteWorkBenchService.removeGoodsCache();
        return R.ok(goodsScopeManager.allotGoodsScope(req));
    }
    @PostMapping("/list")
    @ApiOperation(value = "渠道商列表")
    @JsonFormat
    public TableDataInfo list(@RequestBody SysMemberUserListReq req) {
        startPage(req.getPageNum(), req.getPageSize());
        req.setDeptId(authConfig.getChannelDeptId());
        List<SysChannelUserListDTO> targetList = memberUserService.selectChannelUserList(req);
        // 构建积分数据
        goodsScopeManager.buildOtherInfo(targetList);
        // 设置序号
        AtomicInteger rowNum = new AtomicInteger(1);
        targetList.forEach(item -> {
            item.setSeqNo((req.getPageNum() - 1) * req.getPageSize() + rowNum.get());
            rowNum.getAndIncrement();
        });
        return getDataTable(targetList);
    }

    @Log(title = "渠道商管理-导出", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "渠道商管理-导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody SysMemberReq req) {
        SysMemberUserListReq req1 = new SysMemberUserListReq();
        req1.setDeptId(authConfig.getChannelDeptId());
        List<Long> userIds = Lists.newArrayList();
        if (req.getUserIds() != null && !req.getUserIds().isEmpty()) {
            Arrays.asList(req.getUserIds().split(",")).forEach(userId -> userIds.add(Long.valueOf(userId)));
            req1.setUserIds(userIds);
        }
        if (CollUtil.isEmpty(userIds)) {
            throw new ServiceException("请选择要导出的会员");
        }
        List<SysChannelUserListDTO> targetList = memberUserService.selectChannelUserList(req1);
        // 构建积分数据
        goodsScopeManager.buildOtherInfo(targetList);
        // 设置序号
        AtomicInteger rowNum = new AtomicInteger(1);
        targetList.forEach(item -> {
            item.setSeqNo(rowNum.get());
            rowNum.getAndIncrement();
            item.setFirstNameAndLastName(item.getFirstName() + "." + item.getLastName());
        });
        ExcelUtil<SysChannelUserListDTO> util = new ExcelUtil<SysChannelUserListDTO>(SysChannelUserListDTO.class);
        util.exportExcel(response, targetList, "渠道商数据");
    }
}
